const { resolve, join } = require("path");
const Vueloader = require("vue-loader/lib/plugin");
const HtmlWebpackPlugin = require("html-webpack-plugin"); // 用于把HTML页面放入内存
const CopyWebpackPlugin = require("copy-webpack-plugin");
const { CleanWebpackPlugin } = require("clean-webpack-plugin");
const WebpackBar = require("webpackbar");
const SetScriptTimestampPlugin = require("./plugins/time");
const webpack = require("webpack");
const path = require("path");
const HappyPack = require("happypack");
const happyThreadPool = HappyPack.ThreadPool({ size: 5 });
const htmlplugin = new HtmlWebpackPlugin({
  template: join(process.cwd(), "public/index.html"), // 源头文件
  filename: "index.html", // 生成首页的文件名称
  environment: {
    arrowFunction: true
  } // 打包不使用箭头函数
});
module.exports.bases = {
  // entry: {
  //   vendor: ["axios", "vue", "element-ui"],
  // },
  resolve: {
    extensions: [".js", ".vue", ".json"],
    alias: {
      "@": join(process.cwd(), "src"),
      components: join(process.cwd(), "src/components")
    }
    // modules: [path.resolve(__dirname, 'node_modules')],
    // mainFields: ['main'],
  },
  externals: {
    jquery: "jQuery"
  },
  module: {
    noParse: /jquery|lodash/,
    rules: [
      {
        test: /\.vue$/,
        use: [
          // {
          //   loader: resolve("./liu-cli/loader/index.js"),
          //   options: {
          //     name: "我要替换",
          //   },
          // },
          {
            loader: "vue-loader"
          }
        ]
      },
      {
        test: /\.(js)$/,
        use: [
          {
            loader: "thread-loader", // 开启多进程打包
            options: {
              worker: 3
            }
          },
          {
            loader: "happypack/loader?id=babel"
            // options: {
            //   presets: ["@babel/preset-env"],
            //   cacheDirectory: true, // 启用缓存
            // },
          }
          // "eslint-loader",
        ],
        include: [resolve(__dirname, "src")],
        // include: [resolve('src'), resolve('test'), resolve('node_modules/webpack-dev-server/client')],
        exclude: /node_modules/
      },
      // {
      //   test: /\.(tsx | ts)?$/,
      //   loader: "ts-loader",
      //   exclude: /node_modules/,
      //   options: {
      //     appendTsSuffixTo: [/\.vue$/],
      //   },
      // },
      {
        test: /\.(png|jpe?g|gif|svg|jpg)(\?.*)?$/,
        type: "asset/resource"
      },
      {
        test: /\.(mp4|webm|ogg|mp3|wav|flac|aac)(\?.*)?$/,
        type: "asset"
        // generator: {
        //   // 输出文件位置以及文件名
        //   filename: "[name][hash:8][ext]",
        // },
        // parser: {
        //   dataUrlCondition: {
        //     maxSize: 10 * 1024, // 超过100kb不转 base64
        //   },
        // },
      },
      {
        test: /\.(woff2?|eot|ttf|otf)(\?.*)?$/,
        type: "asset"
      }
    ]
  },
  plugins: [
    htmlplugin,
    // new SetScriptTimestampPlugin(),
    new HappyPack({
      // 用唯一的标识符 id 来代表当前的 HappyPack 是用来处理一类特定的文件
      id: "babel",
      // 如何处理 .js 文件，用法和 Loader 配置中一样
      loaders: ["babel-loader?cacheDirectory"],
      threadPool: happyThreadPool
      // ... 其它配置项
    }),
    new Vueloader(),
    //

    new SetScriptTimestampPlugin({
      title: "刘勇1"
    }),
    new WebpackBar()
    // new webpack.optimize.CommonsChunkPlugin({ names: ["vendor", "runtime"] }),
    // new webpack.DllPlugin({
    //   name: "[name]_lib",
    //   path: "manifest.json",
    // }),
  ],
  optimization: {
    splitChunks: {
      cacheGroups: {
        vendors: {
          name: "chunk-vendors",
          test: /[\\/]node_modules[\\/]/,
          priority: -10,
          chunks: "initial"
        },
        elementUI: {
          name: "chunk-elementUI", // split elementUI into a single package
          priority: 20, // the weight needs to be larger than libs and app or it will be packaged into libs or app
          test: /[\\/]node_modules[\\/]_?element-ui(.*)/ // in order to adapt to cnpm
        },
        three: {
          name: "chunk-three", // split three into a single package
          priority: 20, // the weight needs to be larger than libs and app or it will be packaged into libs or app
          test: /[\\/]node_modules[\\/]_?three(.*)/ // in order to adapt to cnpm
        },
        lodash: {
          name: "chunk-lodash", // split lodash into a single package
          minChunks: 1,
          priority: 20, // the weight needs to be larger than libs and app or it will be packaged into libs or app
          test: /[\\/]node_modules[\\/]_?lodash(.*)/ // in order to adapt to cnpm
        },
        commons: {
          name: "chunk-commons",
          test: resolve("src/components"), // can customize your rules
          minChunks: 1, //  minimum common number
          priority: 5,
          reuseExistingChunk: true
        }
      }
    }
  }
};
